package com.jaryle.quartz.config;


import com.jaryle.quartz.bean.AbstractTask;
import com.jaryle.quartz.util.SpringJobFactory;
import org.quartz.spi.JobFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
/**
 * 描述:
 * Quartz定时任务配置
 *
 * @author jaryle
 * @version V1.0
 * @create 2018-11-28
 */
@Configuration
public class QuartzConfigration{
    private Logger logger = LoggerFactory.getLogger(QuartzConfigration.class);

    /**
     * 配置任务工厂实例
     * @param applicationContext spring上下文实例
     */
    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext){
        //采用自定义任务工厂 整合spring实例来完成构建任务
        SpringJobFactory jobFactory = new SpringJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    /**
     * 创建jobDetailFactoryBean
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        //关联job
        factoryBean.setJobClass(AbstractTask.class);
        return factoryBean;
    }

    /**
     * 配置调度器工厂实例
     * 使用项目数据源作为quartz数据源
     * @param jobFactory 自定义配置任务工厂
     * @param dataSource 数据源实例
     * @return
     * @throws Exception
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, DataSource dataSource) throws Exception
    {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        //将spring管理job自定义工厂交由调度器维护
        schedulerFactoryBean.setJobFactory(jobFactory);
//        //设置覆盖已存在的任务,用于quartz集群,QuartzScheduler 启动时更新己存在的Job
//        schedulerFactoryBean.setOverwriteExistingJobs(true);
//        //项目启动完成后，等待2秒后开始执行调度器初始化
//        schedulerFactoryBean.setStartupDelay(2);
        //设置调度器自动运行
//        schedulerFactoryBean.setAutoStartup(true);
        //设置数据源，使用与项目统一数据源
//        schedulerFactoryBean.setDataSource(dataSource);
        //设置上下文spring bean name
//        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        //设置配置文件位置
//        schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
        return schedulerFactoryBean;
    }

    /**
      * 创建触发器cronTrigger
      * Details：配置定时任务的触发器，也就是什么时候触发执行定时任务

    @Bean
    public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
    CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
    tigger.setJobDetail(jobDetail.getObject());
    // 初始时的cron表达式
    tigger.setCronExpression("0 30 20 * * ?");
    // trigger的name
    tigger.setName("cron-tiggerone");
    return tigger;

    }
     */
}
